package grabana

import (
	
	
	
	
	
	
)

// Option represents an option that can be used to configure a client.
type Option func(client *Client)

type requestModifier func(request *http.Request)

// Client represents a Grafana HTTP client.
type Client struct {
	http             *http.Client
	host             string
	requestModifiers []requestModifier
}

// NewClient creates a new Grafana HTTP client, using an API token.
func ( *http.Client,  string,  ...Option) *Client {
	 := &Client{
		http: ,
		host: ,
	}

	for ,  := range  {
		()
	}

	return 
}

// WithAPIToken sets up the client to use the given token to authenticate.
func ( string) Option {
	return func( *Client) {
		.requestModifiers = append(.requestModifiers, func( *http.Request) {
			.Header.Add("Authorization", "Bearer "+)
		})
	}
}

// WithBasicAuth sets up the client to use the given credentials to authenticate.
func ( string,  string) Option {
	return func( *Client) {
		.requestModifiers = append(.requestModifiers, func( *http.Request) {
			.SetBasicAuth(, )
		})
	}
}

func ( *Client) ( *http.Request) {
	for ,  := range .requestModifiers {
		()
	}
}

func ( Client) ( *http.Response) error {
	,  := io.ReadAll(.Body)
	if  != nil {
		return 
	}

	return fmt.Errorf("could not query grafana: %s (HTTP status %d)", , .StatusCode)
}

func ( Client) ( context.Context,  string) (*http.Response, error) {
	,  := http.NewRequestWithContext(, http.MethodDelete, .url(), nil)
	if  != nil {
		return nil, 
	}

	.modifyRequest()

	return .http.Do()
}

func ( Client) ( context.Context,  string,  string,  []byte) (*http.Response, error) {
	,  := http.NewRequestWithContext(, , .url(), bytes.NewReader())
	if  != nil {
		return nil, 
	}

	.Header.Add("Content-Type", "application/json")
	.modifyRequest()

	return .http.Do()
}

func ( Client) ( context.Context,  string) (*http.Response, error) {
	,  := http.NewRequestWithContext(, http.MethodGet, .url(), nil)
	if  != nil {
		return nil, 
	}

	.modifyRequest()

	return .http.Do()
}

func ( Client) ( string) string {
	return .host + 
}

func decodeJSON( io.Reader,  interface{}) error {
	return json.NewDecoder().Decode()
}